;
(function($,win)
{
	/* @------------------------------------------------------------------------------------@ */
	$(document).ready(function()
	{
		var url = location.search;
		if (url.indexOf("?userUuid=") != -1) {
			var reqPath = basePath+'user/queryUserById'+url;
			var sendBody = '{}';
			send(reqPath, "POST", "application/json", "json", sendBody, function(res)
			{
				if (res.result == '1')
				{
					if (res.data != null)
					{
						var user = res.data;
						$("#user_uuid").val(user.userUuid);
						$("#user_name").val(user.userName);
						$("#pass_word").val(user.password);
						$("#user_phone").val(user.userPhone);
						$("#user_role").val(user.userRole);
					} else
					{
						alert("没有查找到相关数据");
					}
				}
			});
		}else
		{
			//初始化列表
			iniUserData(1,15);
		}
		$('#addnew').click(function()
		{

			window.location.href = jumpPath + 'view/system/user/adduser.jsp';
		});

		// 查询
		$('#btn_search').bind('click', function()
		{
			iniUserData(1,15);
		});

		$('#tbn_save').bind('click', function()
		{
			saveLoginUser();
		});
		
		$('#tbn_update').bind('click', function()
		{
			updateLoginUser();
		});

	});// the end

	/* @-----------------------------------------------------------------------------------------@ */
})(jQuery, window);

function editUser(userId)
{
	console.log("edutUser:"+jumpPath);
	window.location.href = jumpPath + 'view/system/user/edituser.jsp?userUuid='+userId;
}

function del(id)
{
	if (confirm("确定要删除吗？"))
	{
		var reqPath = basePath+'user/deleteUser?userUuid='+id;
		var sendBody = '{"uuid": "'+id+'"}';
		send(reqPath, "POST", "application/json", "json", "{}", function(res)
		{
			if (res.result == '1')
			{
				iniUserData(1,15);
			}
		});
	}
}

function iniUserData(pageNumber,pagesize)
{
	var reqPath = basePath+'user/queryPage';
	var sendBody = '{"curPage": 1,"pageSize": 15';
	var $tab = $('#userDataTables tbody').html("");

	debugger;
	var reqPath = basePath + 'user/queryPage';
	var sendBody = '{"curPage": '+pageNumber+',"pageSize": '+pagesize+'';
	var $tab = $('#userDataTables tbody').html("");
	
	var requestData = $('#frm_login_user').serializeObject();
	requestData = JSON.stringify(requestData);
	sendBody = sendBody+requestData.replace('{',',');

	send(reqPath, "POST", "application/json", "json", sendBody, function(res)
	{

		if (res.result == '1')
		{

			if (res.data.data.length > 0)
			{
				var tpl_tr = $('#tpl_list').html(), rows = '';

				$.each(res.data.data, function(i,n)
				{
					var userType = '普通用户';
					if(null!=n.userRole&&n.userRole==1)
					{
						userType = '管理员';
					}else if (null!=n.userRole&&n.userRole==2)
					{
						userType = '客服';
					}else if (null!=n.userRole&&n.userRole==3)
					{
						userType = '经销商';
					}else if (null!=n.userRole&&n.userRole==4)
					{
						userType = '会员';
					}else
					{
						userType = '其他';
					}
					var operation="<button onclick='editUser(\""+n.userUuid+"\")' type='button' class='btn btn-primary'>编辑</button>"+
								  "<button onclick='del(\""+n.userUuid+"\")' type='button' class='btn btn-primary'>删除</button>";
					rows += tpl_tr.replace(/\$userUuid/g, n.userUuid)
							      .replace(/\$userName/,n.userName)
							      .replace(/\$userPhone/, n.userPhone)
							      .replace(/\$userRole/, userType)
							      .replace(/\$operation/, operation);
				});
				$tab.append(rows);

				// page
				pagefunction(iniUserData,res.data.totalPages,pagesize);

			} else
			{
				$tab.append('<tr><td colspan="7">没有查找到相关数据。</td></tr>');
			}
		}
	});

}

/**
 * 
 * @METHODNAME: saveLoginUser
 * @METDHOD:
 * @Description: 添加登陆用户
 * @author Administrator
 * @date 2015年1月10日 下午5:37:52
 */
function saveLoginUser()
{
	
	var requsetPath = basePath + "user/addUser";
	var requestData = $('#frm_login_user').serializeObject();
	requestData = JSON.stringify(requestData);
	send(requsetPath, "POST", "application/json", "json", requestData,
	function(resultData)
	{

		if (resultData.result == '1')
		{
			// 跳转到列表
			window.location = jumpPath
					+ 'view/system/user/userlist.jsp';
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}


function updateLoginUser()
{
	var requsetPath = basePath + "user/updateUser";
	var requestData = $('#frm_login_user').serializeObject();
	requestData = JSON.stringify(requestData);
	// alert(requestData);
	send(requsetPath, "POST", "application/json", "json", requestData,
	function(resultData)
	{

		if (resultData.result == '1')
		{
			// 跳转到列表
			window.location = jumpPath
					+ 'view/system/user/userlist.jsp';
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}
